<?php
namespace Home\Controller;
use Think\Controller;
use Think\Page;
use Home\Service\YoudaoService;
class IndexController extends BaseController {
    
    
    public function index(){
        
        $banner = banner();
        $cate_list = $this->cate_list();
        $this->accept_lang();
        
        $this->assign('banner',$banner);
        $this->assign('list',$cate_list);
        $this->display();    
    }
    
    public function accept_lang(){
        $ip = get_client_ip();
        $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
        $json = json_decode(file_get_contents($url),true);
        $country_id = $json['data']['country_id'];
        if ($country_id !=='CN') {
            redirect('https://allsins18k.shoplineapp.com');
        }

    }
    

    
    
    /**
     * 品牌介绍
     * 
     * date:2017年10月14日
     * author: EK_熊
     */
    public function brand(){
        $detail = M('Brand')->where(['id'=>1])->find();
        $this->assign('detail',$detail);
        $this->display();
    }
    
    

    
    /**
     * 列表页访问路径
     * @param unknown $tcid
     * date:2017年9月18日
     * author: EK_熊
     */
     public function visit_path($tcid){
         $tcid = I('tcid');
         if (empty($tcid)) $this->ajaxReturn(['msg'=>'参数错误']);
         $termid = M('Terms')->where(['term_id'=>$tcid])->field('name,parent')->find();
         $termpar = M('Terms')->where(['term_id'=>$termid['parent']])->field('name')->find();
         
         $list = [
             ['tag'=>'首页','id'=>''],
             ['tag'=>$termpar['name'],'id'=>$termid['parent']],
             ['tag'=>$termid['name'],'id'=>$tcid],
         ];
         $this->ajaxReturn($list);
     }
    

    
    
    
    
    /**
     * 
     * 
     * date:2017年9月15日
     * author: EK_熊
     */
    public function cate_list(){
        $map = [
            'status'=>1,
            '_string'=>"`url` is null OR `url` = ''",
        ];
        $upload_path = C("TMPL_PARSE_STRING.__UPLOAD__");
        $result = M('terms')->where($map)->field("url,term_id as id,name,created_time,concat('$upload_path',cover) as cover,is_nav,parent as parentid,recommend")->order('listorder')->select();

        $tree = new \Tree();
        $tree->init($result);
        $get_tree = $tree->get_tree_array(0);
        $catetree = array_values($get_tree);
        $catebox=[];
        $probox=[];
        //获取二级菜单，下面的推荐一个商品
        
        $proInfo = $this->getProRecommend();
        
        
       $this->assign('prolist',$proInfo);
       $this->assign('catebox',$catebox);
        return $catetree;
    }
    
    
    public function getProRecommend(){
        $map = [
            'post_status'=>1,
            'recommended'=>1,
            'rela.status'=>1,
        ];
        $join = [
            '__TERM_RELATIONSHIPS__ rela ON pro.id = rela.object_id',
            '__TERMS__ term ON rela.term_id = term.term_id',
        ];
        $field = 'pro.id,term.term_id,smeta,post_title,parent,term.name,rela.status';
        $ret = M('Posts')->alias('pro')->where($map)->join($join)->field($field)->order('recommended,post_modified desc')->select();
        $ret = D('Posts')->format_smeta($ret);
        
        foreach ($ret as $k=>$v) {
            $data[$v['parent']][] = $v; 
        }
        return $data;
    }
    
    
}